#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "commutil.h"


int init_company_attr(MYSQL *conn, company_attr_t **companyattr) {
    const char *comsql = "select fid from "SITE_DBNAME".t5_companyinfo where fstatus = 1";
    int r = mysql_real_query(conn, comsql, strlen(comsql));
    if ( r) {
        fprintf(stderr, "init_company_attr error:%s\n", mysql_error(conn));
    }

    MYSQL_RES *comres = mysql_store_result(conn);
    if ( comres == NULL) {
        fprintf(stderr, "init_company_attr _store_result error:%s\n", mysql_error(conn));
        return -1;
    }

    MYSQL_ROW comrow;
    const char *loomFmt = "select fid, frepeaterid, flcardid from "
                        COMDB_PREFIX"%d.t5_loominfo where fstatus = 1";
    char loomsql[1024];

    while ( (comrow = mysql_fetch_row(comres) )) {
        char *stopptr;
        uint16_t comid = strtol(comrow[0], &stopptr, 10);

        memset(loomsql, 0x00, sizeof(loomsql));
        snprintf(loomsql, sizeof(loomsql) - 1, loomFmt, comid);
        r = mysql_real_query(conn, loomsql, strlen(loomsql));
        if (r) {
            fprintf(stderr, "query company loom info error:%s\n", mysql_error(conn));
            continue;
        }

        MYSQL_RES *res = mysql_store_result(conn);
        uint16_t listlen = sizeof(loomattr_list_t);
        uint16_t attrlen = MAX_COLLECTORNUM * sizeof(loom_attr_t);

        ASSERT(res != NULL);
        MYSQL_ROW row;
        while ( (row = mysql_fetch_row(res)) != NULL ) {
            uint32_t pkid = strtol(row[0], NULL, 10);
            uint16_t repeater = strtol(row[1], NULL, 10);
            uint16_t collector = strtol(row[2], NULL, 10);
            uint8_t modid = repeater % MAX_REPEATERNUM;
            uint16_t modcoll = collector % MAX_COLLECTORNUM;
            company_attr_t *itemcompany = &(*(company_attr_t (*)[MAX_COMPANYNUM][MAX_REPEATERNUM])companyattr)[comid][modid];
            if (pkid < 0 ) {
                fprintf(stderr, "%s\n", "get loom primary key id error!");
            }

            if (itemcompany->elements == 0) {
                itemcompany->repeater = repeater;
                loomattr_list_t *loomlist = (loomattr_list_t *) malloc(listlen);
                loom_attr_t *loomattr = (loom_attr_t*)malloc(attrlen);
                memset(loomlist, 0x00, listlen);
                memset(loomattr, 0x00, attrlen);
                loomattr[modcoll].repeater = repeater;
                loomattr[modcoll].collector = collector;

                loomlist->repeater = repeater;
                loomlist->loomattr = loomattr;
                loomlist->elements = 1;
                loomlist->next = 0x00;

                itemcompany->header = loomlist;
                itemcompany->current = loomlist;
                itemcompany->elements++;
            }
            else {
                loomattr_list_t *itemattr = find_loom_attr(repeater, itemcompany);

                if ( itemattr ) {
                    itemattr->loomattr[modcoll].repeater = repeater;
                    itemattr->loomattr[modcoll].collector = collector;
                    itemattr->elements++;

                }
                else {
                    itemcompany->repeater = repeater;

                    loomattr_list_t *loomlist = (loomattr_list_t *) malloc(listlen);
                    loom_attr_t *loomattr = (loom_attr_t*)malloc(attrlen);
                    memset(loomlist, 0x00, listlen);
                    memset(loomattr, 0x00, attrlen);
                    loomattr[modcoll].repeater = repeater;
                    loomattr[modcoll].collector = collector;

                    loomlist->repeater = repeater;
                    loomlist->loomattr = loomattr;
                    loomlist->elements = 1;
                    loomlist->next = 0x00;

                    itemcompany->header = loomlist;
                    itemcompany->current = loomlist;
                    itemcompany->elements++;
                }

            }
        }
        mysql_free_result(res);
        res = 0x0;

    }
    mysql_free_result(comres);
    comres = 0x0;
    return 0;
}

loomattr_list_t *find_loom_attr(uint16_t repeater, company_attr_t *companyattr) {

    if ( companyattr != NULL ) {
        loomattr_list_t *current = companyattr->header;
        while ( current ) {
            if ( current->repeater == repeater) {
                return current;
            }
            current = current->next;
        }
    }
    return (loomattr_list_t*)NULL;
}

void do_server_log(MYSQL *conn, const char *type, const char *msg) {
    time_t conntm = time(NULL);
    const char *fmt = "insert into t5_serverlog(ftype, faddtm, fdatatm, finfo) "
            "values('%s', %u, %u, '%s')";
    char sqlbuff[1024];
    memset(sqlbuff, 0x00, sizeof(sqlbuff));
    if ( type == 0x0)
        type = "<unknow>";
    if (msg == 0x0)
        msg = "<unknow>";

    snprintf(sqlbuff, sizeof(sqlbuff) - 1, fmt, type, conntm, conntm, msg);

    int ret = mysql_real_query(conn, sqlbuff, strlen(sqlbuff));
 	if (ret != 0) {
		fprintf(stderr, "do_server_log save db error!\n");
	}
}
